﻿/*----------------------------------------------------------------
// Copyright (C) 2012
// 版权所有
//
// 文件名：OrderOnline_City.cs
// 文件功能描述：
//
//
// 创建标识：Libing 2012-05-10 00:53:14 星期四
//
// 修改标识：
// 修改描述：
//----------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using WMS.DBUtility;

namespace WMS.SQLServerDAL
{
	public class OrderOnline_City : WMS.IDAL.IOrderOnline_City
	{
		#region 基本方法

		/// <summary>
		/// 添加实体
		/// </summary>
		/// <param name="orderOnlineCity">类型为 WMS.Model.OrderOnline_City 的实体</param>
		/// <returns></returns>
		public bool Insert(WMS.Model.OrderOnline_City orderOnlineCity)
		{
			SqlParameter[] parameters = {
				new SqlParameter("@ID", SqlDbType.Char, 6),
				new SqlParameter("@CityName", SqlDbType.VarChar, 50),
				new SqlParameter("@ProvinceID", SqlDbType.Char, 6)
			};
			parameters[0].Value = orderOnlineCity.Id;
			parameters[1].Value = orderOnlineCity.CityName;
			parameters[2].Value = orderOnlineCity.ProvinceID;

			try
			{
				int result = DbHelperSQL.ExecuteStoredProcNonQuery("[OrderOnline_City_Insert]", parameters);

				return result > 0;
			}
			catch(SqlException ex)
			{
				throw new Exception(ex.Message, ex);
			}
		}

		/// <summary>
		/// 修改实体
		/// </summary>
		/// <param name="orderOnlineCity">类型为 WMS.Model.OrderOnline_City 的实体</param>
		/// <returns></returns>
		public bool Update(WMS.Model.OrderOnline_City orderOnlineCity)
		{
			SqlParameter[] parameters = {
				new SqlParameter("@ID", SqlDbType.Char, 6),
				new SqlParameter("@CityName", SqlDbType.VarChar, 50),
				new SqlParameter("@ProvinceID", SqlDbType.Char, 6)
			};
			parameters[0].Value = orderOnlineCity.Id;
			parameters[1].Value = orderOnlineCity.CityName;
			parameters[2].Value = orderOnlineCity.ProvinceID;

			try
			{
				return DbHelperSQL.ExecuteStoredProcNonQuery("[OrderOnline_City_Update]", parameters) > 0;
			}
			catch(SqlException ex)
			{
				throw new Exception(ex.Message, ex);
			}
		}

		/// <summary>
		/// 最小修改实体
		/// </summary>
		/// <param name="model">类型为WMS.Model.OrderOnline_City的实体</param>
		/// <returns></returns>
		public bool UpdateMinimal(WMS.Model.OrderOnline_City orderOnlineCity)
		{
			SqlCommand cmd = new SqlCommand("[OrderOnline_City_UpdateMinimal]");

			if (orderOnlineCity.Id != string.Empty)
			{
				cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Char, 6)).Value = orderOnlineCity.Id;
			}

			if (orderOnlineCity.CityName != string.Empty)
			{
				cmd.Parameters.Add(new SqlParameter("@CityName", SqlDbType.VarChar, 50)).Value = orderOnlineCity.CityName;
			}

			if (orderOnlineCity.ProvinceID != string.Empty)
			{
				cmd.Parameters.Add(new SqlParameter("@ProvinceID", SqlDbType.Char, 6)).Value = orderOnlineCity.ProvinceID;
			}

			try
			{
				return DbHelperSQL.ExecuteStoredProcNonQuery(cmd) > 0;
			}
			catch (SqlException ex)
			{
				throw new Exception(ex.Message, ex);
			}
		}

		/// <summary>
		/// 删除实体(根据主键 ID)
		/// </summary>
		/// <param name="id">Id</param>
		/// <returns></returns>
		public bool DeleteByID(string id)
		{
			SqlParameter[] parameters = {
				new SqlParameter("@ID", SqlDbType.Char,6)
			};
			parameters[0].Value = id;

			try
			{
				return DbHelperSQL.ExecuteStoredProcNonQuery("[OrderOnline_City_DeleteByID]", parameters) > 0;
			}
			catch(Exception ex)
			{
				throw new Exception(ex.Message, ex);
			}
		}

		/// <summary>
		/// 删除实体(指定条件)
		/// </summary>
		/// <param name="whereClause">WHERE语句</param>
		/// <returns></returns>
		public bool DeleteDynamic(string whereClause)
		{
			SqlParameter[] parameters = {
				new SqlParameter("@WhereClause", SqlDbType.NVarChar,2000)
			};
			parameters[0].Value = whereClause;

			try
			{
				return DbHelperSQL.ExecuteStoredProcNonQuery("[OrderOnline_City_DeleteDynamic]", parameters) > 0;
			}
			catch(Exception ex)
			{
				throw new Exception(ex.Message, ex);
			}
		}

		/// <summary>
		/// 获取实体(根据主键 ID)
		/// </summary>
		/// <param name="id">Id</param>
		/// <returns></returns>
		public WMS.Model.OrderOnline_City GetByID(string id)
		{
			SqlParameter[] parameters = {
				new SqlParameter("@ID", SqlDbType.Char,6)
			};
			parameters[0].Value = id;

			try
			{
				List<WMS.Model.OrderOnline_City> orderOnlineCityList = new List<WMS.Model.OrderOnline_City>();

				SqlDataReader dr = DbHelperSQL.ExecuteStoredProcReader("[OrderOnline_City_GetByID]", parameters);
				while (dr.Read())
				{
					WMS.Model.OrderOnline_City orderOnlineCity = new WMS.Model.OrderOnline_City();

					orderOnlineCity.Id = Convert.ToString(dr["ID"]);
					orderOnlineCity.CityName = Convert.ToString(dr["CityName"]);
					orderOnlineCity.ProvinceID = Convert.ToString(dr["ProvinceID"]);
					orderOnlineCity.ModelState = WMS.Model.Enum.ModelState.None;

					orderOnlineCityList.Add(orderOnlineCity);
				}

				// Close the dr, which will cause the connection to be closed as well.
				dr.Close();

				if(orderOnlineCityList.Count > 0)
				{
					return orderOnlineCityList[0];
				}
				else
				{
					return null;
				}
			}
			catch(Exception ex)
			{
				throw new Exception(ex.Message, ex);
			}
		}

		#endregion

		#region	公共方法

		/// <summary>
		/// 获取实体列表(指定数目和查询条件)
		/// </summary>
		/// <param name="topClause">指定实体数</param>
		/// <param name="whereClause">WHERE语句</param>
		/// <returns></returns>
		public List<WMS.Model.OrderOnline_City> Get(int topClause, string whereClause)
		{
			SqlParameter[] parameters = {
				new SqlParameter("@TopClause", SqlDbType.Int,4),
				new SqlParameter("@WhereClause", SqlDbType.NVarChar,2000)
			};
			parameters[0].Value = topClause;
			parameters[1].Value = whereClause;

			try
			{
				List<WMS.Model.OrderOnline_City> orderOnlineCityList = new List<WMS.Model.OrderOnline_City>();

				SqlDataReader dr = DbHelperSQL.ExecuteStoredProcReader("[OrderOnline_City_GetDynamic]", parameters);
				while (dr.Read())
				{
					WMS.Model.OrderOnline_City orderOnlineCity = new WMS.Model.OrderOnline_City();

					orderOnlineCity.Id = Convert.ToString(dr["ID"]);
					orderOnlineCity.CityName = Convert.ToString(dr["CityName"]);
					orderOnlineCity.ProvinceID = Convert.ToString(dr["ProvinceID"]);
					orderOnlineCity.ModelState = WMS.Model.Enum.ModelState.None;

					orderOnlineCityList.Add(orderOnlineCity);
				}

				// Close the dr, which will cause the connection to be closed as well.
				dr.Close();

				return orderOnlineCityList;
			}
			catch(Exception ex)
			{
				throw new Exception(ex.Message, ex);
			}
		}

		/// <summary>
		/// 获取实体列表(指定查询条件)
		/// </summary>
		/// <param name="whereClause">WHERE语句</param>
		/// <returns></returns>
		public List<WMS.Model.OrderOnline_City> Get(string whereClause)
		{
			return Get(0, whereClause);
		}

		/// <summary>
		/// 获取分页实体列表
		/// </summary>
		/// <param name="whereClause">WHERE 语句</param>
		/// <param name="orderByClause">ORDER BY 语句</param>
		/// <param name="pageIndex">分页索引</param>
		/// <param name="pageSize">分页大小(每页显示的记录数)</param>
		/// <param name="totalRecords">记录总数</param>
		/// <returns></returns>
		public List<WMS.Model.OrderOnline_City> GetPaged(string whereClause, string orderBy, int pageIndex, int pageSize, out int totalRecords)
		{
			SqlParameter[] parameters = {
				new SqlParameter("@WhereClause", SqlDbType.NVarChar, 2000),
				new SqlParameter("@OrderBy", SqlDbType.NVarChar, 1000),
				new SqlParameter("@PageIndex", SqlDbType.Int, 4),
				new SqlParameter("@PageSize", SqlDbType.Int, 4)
			};
			parameters[0].Value = whereClause;
			parameters[1].Value = orderBy;
			parameters[2].Value = pageIndex;
			parameters[3].Value = pageSize;

			try
			{
				List<WMS.Model.OrderOnline_City> orderOnlineCityList = new List<WMS.Model.OrderOnline_City>();

				SqlDataReader dr = DbHelperSQL.ExecuteStoredProcReader("[OrderOnline_City_GetPaged]", parameters);
				while (dr.Read())
				{
					WMS.Model.OrderOnline_City orderOnlineCity = new WMS.Model.OrderOnline_City();

					orderOnlineCity.Id = Convert.ToString(dr["ID"]);
					orderOnlineCity.CityName = Convert.ToString(dr["CityName"]);
					orderOnlineCity.ProvinceID = Convert.ToString(dr["ProvinceID"]);
					orderOnlineCity.ModelState = WMS.Model.Enum.ModelState.None;

					orderOnlineCityList.Add(orderOnlineCity);
				}

				totalRecords = orderOnlineCityList.Count;

				if (dr.NextResult())
				{
					if (dr.Read())
					{
						totalRecords = Convert.ToInt32(dr[0]);
					}
				}

				// Close the dr, which will cause the connection to be closed as well.
				dr.Close();

				return orderOnlineCityList;
			}
			catch(Exception ex)
			{
				throw new Exception(ex.Message, ex);
			}
		}

		#endregion

		#region 自定义方法

        /// <summary>
        /// 获取实体列表(指定查询条件)
        /// </summary>
        /// <param name="provinceID">省份ID</param>
        /// <returns></returns>
        public List<WMS.Model.OrderOnline_City> GetByProvinceID(string provinceID)
        {
            return Get(String.Format("ProvinceID = '{0}' ORDER BY ID", provinceID));
        }

		#endregion
	}
}
